package main

import (
	"fmt"
	"log"
	"net/http"
	"strings"
	"time"
)

type Myhandler int

func sayhelloName(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	fmt.Println(r.Form)
	fmt.Println("path", r.URL.Path)
	fmt.Println("scheme", r.URL.Scheme)
	fmt.Println(r.Form["url_long"])
	for k, v := range r.Form {
		fmt.Println("key:", k)
		fmt.Println("val:", strings.Join(v, ""))
	}
	fmt.Fprintf(w, "Hello world!")
}

// 接口实现
func (m *Myhandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	fmt.Println(r.Form)
	fmt.Println("path", r.URL.Path)
	fmt.Println("scheme", r.URL.Scheme)
	fmt.Println(r.Form["url_long"])
	for k, v := range r.Form {
		fmt.Println("key:", k)
		fmt.Println("val:", strings.Join(v, ""))
	}
	fmt.Fprintf(w, "Hello world!")
}

func main() {

	// 接口实现
	//	f := Myhandler(2)
	//	var my http.Handler = &f

	httpServer := &http.Server{
		Addr: ":8080",
		//		Handler:        my,
		ReadTimeout:    10 * time.Second,
		WriteTimeout:   10 * time.Second,
		MaxHeaderBytes: 1 << 20,
	}

	// 第一种方法
	//	http.HandleFunc("/show", sayhelloName)

	// 第二种方法 type HandlerFunc func(ResponseWriter, *Request)
	//	http.Handle("/show", http.HandlerFunc(sayhelloName))

	// 第三种
	routerlist := http.NewServeMux()
	// 建路由
	routerlist.HandleFunc("/show", sayhelloName)
	routerlist.HandleFunc("/list", sayhelloName)
	httpServer.Handler = routerlist //加载路由表

	log.Fatal(httpServer.ListenAndServe())

	//	http.Handle("/foo", fooHandler)
	//	http.HandleFunc("/bar", func(w http.ResponseWriter, r *http.Request) {
	//		fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
	//	})
	//	log.Fatal(http.ListenAndServe(":8080", nil))
}
